package com.example.sprintbootarithmetic;

import cn.hutool.json.JSONUtil;

/**
 * @ClassName arithmetic
 * @Description TODO
 * @Author 35252
 * @Date 2023/3/28 15:00
 * @Version 1.0
 */
public class arithmetic {
    public static NodeLink nodeLinkone = new NodeLink("1", null);
    public static NodeLink nodeLinktwo = new NodeLink("1", null);

    public arithmetic() {
        nodeLinkone.setNodeLink(new NodeLink("2", new NodeLink("4", null)));
        nodeLinktwo.setNodeLink(new NodeLink("3", null));
    }


    /**
     * 合并
     *
     * @param one
     * @param two
     * @return
     */
    public NodeLink merge(NodeLink one, NodeLink two) {
        NodeLink merger = null;
        if (one.getValue().compareTo(two.getValue()) >= 0) {
            merger = two;
            while (one != null) {
                while (merger.getNodeLink() != null) {
                    if (one.getValue().compareTo(merger.getValue()) > 0) {
                        NodeLink twonext = new NodeLink();
                        twonext.setNodeLink(merger.getNodeLink());
                        twonext.setValue(merger.getValue());
                        //返回的为比较的two链表中比当前one大的那个数值
                        String val = bj(one, twonext);
                        while (!(merger.getNodeLink().getValue().compareTo(val) > 0) || merger.getNodeLink() != null) {
                            merger = merger.getNodeLink();
                            if(merger.getNodeLink()==null){
                                break;
                            }
                        }
                        NodeLink link = merger.getNodeLink();
                        NodeLink node1 = new NodeLink();
                        node1.setValue(one.getValue());
                        node1.setNodeLink(one.getNodeLink());
                        merger.setNodeLink(node1);
                        merger.getNodeLink().setNodeLink(link);
                    } else if (one.getValue().compareTo(merger.getValue()) == 0) {
                        NodeLink link = merger.getNodeLink();
                        NodeLink node2 = new NodeLink();
                        node2.setValue(one.getValue());
                        node2.setNodeLink(one.getNodeLink());
                        merger.setNodeLink(node2);
                        merger.getNodeLink().setNodeLink(link);
                    }
                    break;
                }
                one = one.getNodeLink();
            }
        } else {
            merger = one;
            //代码同上
        }

        return two;
    }

    public String bj(NodeLink one, NodeLink two) {
        int i = one.getValue().compareTo(two.getValue());
        if (i > 0 && two.getNodeLink() != null) {
            two = two.getNodeLink();
            bj(one, two);
        }
        return two.getValue();
    }

    public static void main(String[] args) {
        arithmetic arithmetic = new arithmetic();
        NodeLink link = arithmetic.merge(nodeLinkone, nodeLinktwo);
        System.out.println(JSONUtil.toJsonPrettyStr(link));
    }
}
